package com.java.modebing.test.util;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Map;

public class CommonUtils {


    public static final String REPEAT_PARAMS = "repeatParams";

    public static final String REPEAT_TIME = "repeatTime";

    public static final String REPEAT_SUBMIT_KEY = "repeat_submit:";

    /**
     * 判断参数是否相同
     */
    public static boolean compareParams(Map<String, Object> nowMap, Map<String, Object> preMap) {
        String nowParams = (String) nowMap.get(REPEAT_PARAMS);
        String preParams = (String) preMap.get(REPEAT_PARAMS);
        return nowParams.equals(preParams);
    }

    /**
     * 判断两次间隔时间
     */
    public static boolean compareTime(Map<String, Object> nowMap, Map<String, Object> preMap, int interval) {
        long time1 = (Long) nowMap.get(REPEAT_TIME);
        long time2 = (Long) preMap.get(REPEAT_TIME);
        if ((time1 - time2) < interval) {
            return true;
        }
        return false;
    }
    
    public static String toJSONString(Object object) {
        if (object != null) {
            try {
                return new ObjectMapper().writeValueAsString(object);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

